package com.lanp.native_test.ExecutorsRun;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorTest {
    public static void main(String[] args) {
        /* -------------------------- 创建固定数量的线程池 ------------------------- */
        ExecutorService executorSer = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-newFixedThreadPool:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorSer.execute(runnable);
        }
        executorSer.shutdown();
        System.out.println("停止线程池");
        /* -------------------------- 创建只有一个线程的线程池 ------------------------- */
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-newSingleThreadExecutor:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorService.execute(runnable);
        }
        executorService.shutdown();
        /* -------------------------- 创建只有一个线程的线程池 ------------------------- */
        ExecutorService executorCache = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-executorCache:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorCache.execute(runnable);
        }
        executorCache.shutdown();
        /* -------------------------- 创建指定核心线程数，最大线程池数量是int最大值的线程池 ------------------------- */
        ExecutorService executorNScheduled = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-newScheduledThreadPool:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorNScheduled.execute(runnable);
        }
        executorNScheduled.shutdown();
        /* -------------------------- 创建单核心线程数，最大线程池数量是int最大值的线程池 ------------------------- */
        ExecutorService executorSingleScheduled = Executors.newSingleThreadScheduledExecutor();
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-newSingleThreadScheduledExecutor:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorSingleScheduled.execute(runnable);
        }
        executorSingleScheduled.shutdown();
        /* -------------------------- 创建cpu线程数的线程池 ------------------------- */
        ExecutorService executorWorkStealing = Executors.newWorkStealingPool();
        for (int i = 0; i < 10; i++) {
            final int fi = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(fi + "-索引-newWorkStealingPool:" + RandomStringUtils.randomAlphabetic(10));
                }
            };
            executorWorkStealing.execute(runnable);
        }
        executorWorkStealing.shutdown();
    }
}
